package com.ry.bhwktapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.NavController
import androidx.navigation.NavGraph
import androidx.navigation.NavGraphNavigator
import androidx.navigation.NavigatorProvider
import androidx.navigation.fragment.NavHostFragment
import com.bbq.base.base.BaseActivity
import com.bbq.base.base.BaseVMActivity
import com.bbq.base.utils.FixFragmentNavigator
import com.ry.bhwktapp.databinding.ActivityMainBinding
import com.ry.bhwktapp.home.ui.FaqFragment
import com.ry.bhwktapp.home.ui.HomeFragment
import com.ry.bhwktapp.home.ui.NavigationFragment
import com.ry.bhwktapp.home.ui.UserFragment
import com.ry.bhwktapp.ui.AboutActivity
import com.ry.bhwktapp.utils.TTsUtil

class MainActivity : BaseVMActivity<ActivityMainBinding>(){

private var navController:NavController?=null
    override fun initView(savedInstanceState: Bundle?) {
        if (savedInstanceState==null) {
            setupBottomNavigationBar()
        }
        TTsUtil.getInstance().speak("你好小泥鳅");
        TTsUtil.getInstance().speak("你好小泥鳅");

    }
    private fun setupBottomNavigationBar() {
        var navHostFragment:NavHostFragment =
            supportFragmentManager.findFragmentById(R.id.fragment_nav) as NavHostFragment
         navController = navHostFragment.navController
//        创建自定义导航器
        val fixFragmentNavigator =
            FixFragmentNavigator(this, navHostFragment.childFragmentManager, navHostFragment.id)
        //获取导航器提供者
        val navigatorProvider = navController!!.navigatorProvider
      //把自定义的Fragment导航器添加进去
        navigatorProvider.addNavigator(fixFragmentNavigator)



        //手动创建导航图
       val navGraph =  initNavGraph(navigatorProvider,fixFragmentNavigator)
        //设置导航图
        navController!!.setGraph(navGraph)

//        NavigationUI.setupWithNavController(mBinding.bottomNav, navController)
        //底部导航设置点击事件
        //底部导航设置点击事件
        mBinding.bottomNav.setOnNavigationItemSelectedListener { item ->
            navController!!.navigate(item.getItemId())
            true
        }
        mBinding.bottomNav.itemTextAppearanceActive = R.style.bottom_selected_text;
        mBinding.bottomNav.itemTextAppearanceInactive = R.style.bottom_normal_text;
    }


    private fun initNavGraph(navigatorProvider: NavigatorProvider, fixFragmentNavigator: FixFragmentNavigator): NavGraph {
        val navGraph = NavGraph(NavGraphNavigator(navigatorProvider))

        //用自定义的导航器来创建目的地
        val destination1 = fixFragmentNavigator.createDestination()
        destination1.id = R.id.navigation_home
        destination1.className = HomeFragment::class.java.canonicalName
        destination1.label = resources.getString(R.string.HomeFragment)
        navGraph.addDestination(destination1)

        val destination2 = fixFragmentNavigator.createDestination()
        destination2.id = R.id.navigation_faq
        destination2.className = FaqFragment::class.java.canonicalName
        destination2.label = resources.getString(R.string.FaqFragment)
        navGraph.addDestination(destination2)

        val destination3 = fixFragmentNavigator.createDestination()
        destination3.id = R.id.navigation_navi
        destination3.className = NavigationFragment::class.java.canonicalName
        destination3.label = resources.getString(R.string.NavigationFragment)
        navGraph.addDestination(destination3)


        val destination5 = fixFragmentNavigator.createDestination()
        destination5.id = R.id.navigation_mine
        destination5.className = UserFragment::class.java.canonicalName
        destination5.label = resources.getString(R.string.UserFragment)
        navGraph.addDestination(destination5)

        navGraph.startDestination = R.id.navigation_home
        return navGraph
    }

    override fun getLayoutId(): Int {
    return R.layout.activity_main
    }


}